/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.devoxy.roboeye.R;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 04.05.11
 * Time: 3:19
 */
public class DialogHelper {

    /**
     * Creates simple dialog with {@link android.widget.EditText} and two {@link Button}s.
     * After execution of handler dialog will be dismissed
     *
     * @param context        context
     * @param titleId        title of dialog
     * @param hintId         hint of {@link android.widget.EditText} in dialog
     * @param cancelListener cancel listener, may be <code>null<code/>
     * @param okListener     ok listener, may be <code>null<code/>
     * @return created {@link Dialog}
     */
    public static Dialog showEditTextDialog(Context context,
                                            int titleId,
                                            int hintId,
                                            DialogInterface.OnClickListener cancelListener,
                                            DialogInterface.OnClickListener okListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.icon);
        builder.setTitle(titleId);

        View view = View.inflate(context, R.layout.edit_dialog, null);
        ((EditText) view.findViewById(R.id.value)).setHint(context.getString(hintId));
        builder.setView(view);

        builder.setPositiveButton(R.string.ok, okListener);
        builder.setNegativeButton(R.string.cancel, cancelListener);

        return builder.create();
    }

    public static Dialog cancelAllTasksDialog(Context context,
                                              DialogInterface.OnClickListener positiveHandler,
                                              DialogInterface.OnClickListener negativeHandler) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.icon);
        builder.setTitle(R.string.attention);
        builder.setMessage(R.string.are_you_sure);
        builder.setPositiveButton(R.string.yes, positiveHandler);
        builder.setNegativeButton(R.string.no, negativeHandler);
        return builder.create();
    }

    public static Dialog aboutDialog(Context context,
                                     DialogInterface.OnClickListener okHandler) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View about = View.inflate(context, R.layout.about, null);
        // TODO setup width and height
        builder.setView(about);
        builder.setNeutralButton(R.string.ok, okHandler);
        return builder.create();
    }

    public static Dialog appCrashedDialog(Context context, boolean isCrashReportsSwitchedOn) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.icon);
        builder.setTitle(R.string.sorry);

        if (isCrashReportsSwitchedOn) {
            builder.setMessage(R.string.app_crashed);
        } else {
            CharSequence msg = context.getString(R.string.app_crashed) + context.getString(R.string.you_can_help_improve);
            builder.setMessage(msg);
        }

        builder.setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return builder.create();
    }

}
